package com.ysalliance.getFan.organization;

import android.app.ActivityManager;
import android.app.Application;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Environment;
import android.util.Log;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.Utils;
import com.google.gson.Gson;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;
import com.umeng.message.UTrack;
import com.umeng.message.UmengMessageHandler;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.entity.UMessage;
import com.umeng.socialize.PlatformConfig;

import org.litepal.LitePal;
import org.litepal.LitePalApplication;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import butterknife.ButterKnife;

public class MyApplication extends Application {


    //    友盟分享
    {
        PlatformConfig.setWeixin("wx8e36029140d97515", "831d2f1370de6c97984743bdd93b2d58");
//        PlatformConfig.setQQZone("1106167537", "hvEyNHoUcJaY70Pa");
//        PlatformConfig.setSinaWeibo("1482710371", "57f8d9362afe497efd9d255702528065","http://sns.whalecloud.com");
    }


    /**
     * bugly      发布时设置为false
     * logutils   发布时设置为false
     * umeng      发布时设置为false
     * */
    private boolean is_debug = true;   //bugly 发布false   umeng  发布false   logUtils  发布false

//    private String faner_channel  = "huawei";
//    private String faner_channel  = "yingyongbao";
//    private String faner_channel  = "xiaomi";
//    private String faner_channel  = "oppo";
//    private String faner_channel  = "vivo";
    private String faner_channel  = "official";
//


    private static  MyApplication sMyApp;



    @Override
    public void onCreate() {
        super.onCreate();
        sMyApp = this;

        /** 柯基工具 */
        Utils.init(this);
        is_debug = AppUtils.isAppDebug();


        /** 日志打印 */
        LogUtils.getConfig().setConsoleSwitch(is_debug);

        /**   数据库litepal初始化    */
        LitePal.initialize(this);



        /**   黄油刀    */
        ButterKnife.setDebug(is_debug);



        /**
         * 友盟分享
         * 初始化common库
         * 参数1:上下文，不能为空
         * 参数2:APPKEY
         * 参数3:Channel ID
         * 参数4:设备类型，UMConfigure.DEVICE_TYPE_PHONE为手机、UMConfigure.DEVICE_TYPE_BOX为盒子，默认为手机
         * 参数5:Push推送业务的secret
         */
        UMConfigure.init(this, "5d19ba7f0cafb2cb0a00119f", faner_channel, UMConfigure.DEVICE_TYPE_PHONE, "c74d10d57e9a4b899de31bf55cef779d");
        /**
         * 设置组件化的Log开关
         * 参数: boolean 默认为false，如需查看LOG设置为true
         */
        UMConfigure.setLogEnabled(is_debug);

        /** 友盟推送 */
        //默认是开启的
        if(LucasUtil.pref_get(sMyApp,"close_push").equals("-1")){
            PushAgent mPushAgent = PushAgent.getInstance(this);
            //注册推送服务，每次调用register方法都会回调该接口
            mPushAgent.register(new IUmengRegisterCallback() {
                @Override
                public void onSuccess(String deviceToken) {

                    // TODO: 2020-02-28
                    //注册成功会返回device token
//                    LogUtils.e("注册友盟推送：deviceToken：", deviceToken, LucasUtil.getUser(sMyApp).getId());

//                    if(LucasUtil.isLogin(sMyApp)){
//                        mPushAgent.addAlias(LucasUtil.getUser(sMyApp).getId(), "umpush", new UTrack.ICallBack() {
//                            @Override
//                            public void onMessage(boolean isSuccess, String message) {
//                                Log.e("注册友盟推送", "onMessage: 增加alias成功   "+isSuccess+message );
//                            }
//                        });
//                    }

                }
                @Override
                public void onFailure(String s, String s1) {

                }
            });
            /**   通知点击事件的自定义   */
            UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler(){
                @Override
                public void launchApp(Context context, UMessage msg) {
                    // TODO: 2020-02-28
//                    PushManager.deal_push_msg(msg);
                }

                @Override
                public void openActivity(Context context, UMessage msg) {
                    // TODO: 2020-02-28
//                    PushManager.deal_push_msg(msg);
                }

                @Override
                public void dealWithCustomAction(Context context, UMessage msg) {
                    // TODO: 2020-02-28
//                    PushManager.deal_push_msg(msg);
                }
            };
            /**   通知的回调方法   */
            UmengMessageHandler messageHandler = new UmengMessageHandler() {
                /**
                 * 通知的回调方法
                 * @param context
                 * @param msg
                 */
                @Override
                public void dealWithNotificationMessage(Context context, UMessage msg) {
                    //调用super则会走通知展示流程，不调用super则不展示通知()
                    LogUtils.e("收到的友盟推送", "dealWithNotificationMessage: 获得推送的消息"+ msg.getRaw().toString());
                    super.dealWithNotificationMessage(context, msg);

                }
                @Override
                public Notification getNotification(Context context, UMessage msg) {
                    return getNotify(msg);
                }
            };
            mPushAgent.setNotificationClickHandler(notificationClickHandler);
            mPushAgent.setMessageHandler(messageHandler);
        }







    }




    /**
     * 自定义通知栏样式
     * @author lucas
     * created at 2019/9/5 6:15 PM
     */
    public Notification  getNotify(UMessage msg){
        // TODO: 2020-02-28   收到通知消息后的自定义处理  需要你构造通知栏
        return null;

//        LogUtils.e("收到推送后自定义通知栏进行通知，收到的推送通知消息：", msg.getRaw().toString());
//        Gson gson = new Gson();
//        UmPushData umPushData = gson.fromJson(msg.getRaw().toString(),UmPushData.class);
//
//        //收到推送判断下是不是要建立长链接
//        if (umPushData.getExtra().getClassify() == 5) {
//            One2OneManager.getInstance().check121();
//            EventBus.getDefault().postSticky("fresh");
//        }
//
//
//        if (Build.VERSION.SDK_INT >= 26) {
//            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//            NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_HIGH);
//            if (manager != null) {
//                manager.createNotificationChannel(channel);
//            }
//            Notification.Builder builder = new Notification.Builder(sMyApp, "channel_id");
//            builder.setSmallIcon(R.mipmap.fanerlogo)
//                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.fanerlogo))
//                    .setWhen(System.currentTimeMillis())
//                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
//                    .setContentTitle(umPushData.getBody().getTitle())
//                    .setContentText(umPushData.getBody().getText())
//                    .setAutoCancel(true);
//            return builder.build();
//        } else {
//            Notification.Builder builder = new Notification.Builder(sMyApp);
//            builder.setSmallIcon(R.mipmap.fanerlogo)
//                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.fanerlogo))
//                    .setWhen(System.currentTimeMillis())
//                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
//                    .setContentTitle(umPushData.getBody().getTitle())
//                    .setContentText(umPushData.getBody().getText())
//                    .setAutoCancel(true);
//            return builder.build();
//        }
    }






    public static String getCurProcessName(Context context) {

        int pid = android.os.Process.myPid();

        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);

        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager
                .getRunningAppProcesses()) {

            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }


}
